import json,os

def singleton(cls):
    _instance = {}
    def getInstance(*args,**kwArgs):
        if cls not in _instance:
            _instance[cls] = cls(*args,**kwArgs)
        return _instance[cls]
    return getInstance


@singleton
class ExportSetting():

    def __init__(self,jsonFile:str) -> None:
        self.usePB = None #是否使用protobuff格式导出没一行数据,false就是使用二进制;一开始就需要确定格式,中途修改需要全部表导一遍
        self.forceGen = None
        self.unWriteDFVal = None #当usePB=false 也就二进制格式 是否不写入默认值的字段,例如字段数据类型是字符串但是是空串 则不写入 是数字 但是值是0 则不写入 
        self.excelRoot = None 
        self.protoRoot = None #输出 .proto 文件的目录
        self.configModule = None #模块名，也是最后生成.proto文件的名称 .pb文件的名称
        self.md5MetaFile = None
        self.configMetaFile = None
        self.outPut = None
        self.msgTemp = None
        self.protoTemp = None
        self.excelExt = None #excel文件的后缀
        self.pbFile = None #最终导出的二进制格式文件
        self.sheetPbRoot = None
        self.configPbFile = None #导出的.proto文件
        self.dataExt = None #最终生成的配置文件后缀
        self._loadJson(jsonFile)
    
    def _loadJson(self,jsonFile):
        with open(jsonFile,encoding="utf8") as f:
            jsonObj = json.load(f)
            for key in jsonObj.keys():
                if hasattr(self,key):
                    setattr(self,key,jsonObj[key])
        
        pbName = self.configModule + self.dataExt 
        self.pbFile = os.path.join(self.outPut,pbName)
        self.sheetPbRoot = os.path.join(self.outPut,"sheetpb")
        self.configPbFile = os.path.join(self.protoRoot,f"{self.configModule}.proto") 
        
    
    